// pages/indent/indent.js
var orderAllUrl = require('../../config').orderAllUrl;
var date = require('../../utils/util.js');
var dealOrderUrl = require('../../config').dealOrderUrl;
var app = getApp();
Page({

  data: {
    tabNav: [
       {
        type: 0,
        title: '代付款'
      }, {
        type: 1,
        title: '待发货'
      }
      , {
        type: 2,
        title: '待收货'
      }
      , {
        type: 3,
        title: '已完成'
      }
    ],
    currentTab: 0,
  },
  onLoad: function (options) {
    var that = this;
    var type = options.type;
    var dishList = app.data.dishList;
    wx.request({
      url: orderAllUrl,
      method: 'get',
      data: {
        skey: wx.getStorageSync("skey")
      },
      success: function (res) {
        var orderList = res.data.orderList;
        var noPayList = [];
        var waitDeliverList = [];
        var waitTakeDeliverList = [];
        var completeList = [];
        var showList=[];
        for (var i = 0; i < orderList.length;i++){
          var dish = orderList[i].dish;
          for (var j = 0; j < dish.length; j++) {
            for (var k = 0; k < dishList.length; k++) {
              if (dishList[k].dish.id == dish[j].id) {
                orderList[i].dish[j].icon= dishList[k].icon
              }
            }
          }
          if (orderList[i].order.state === 1 || orderList[i].order.state === 2){
            orderList[i].order.timeStart = date.formatTime(new Date(orderList[i].order.timeStart))
            noPayList.push(orderList[i])
          }
          if (orderList[i].order.state === 3) {
            waitDeliverList.push(orderList[i])
          }
          if (orderList[i].order.state === 4) {
            waitTakeDeliverList.push(orderList[i])
          }
          if (orderList[i].order.state === 5) {
            completeList.push(orderList[i])
          }
          
        }
        that.setData({
          orderList: orderList,
          completeList: completeList,
          noPayList: noPayList,
          waitTakeDeliverList: waitTakeDeliverList,
          waitDeliverList: waitDeliverList,
          currentTab: type
        })
        if (type == 0) {
          showList = that.data.noPayList
        }
        if (type == 1) {
          showList = that.data.waitDeliverList
        }
        if (type == 2) {
          showList = that.data.waitTakeDeliverList
        }
        if (type == 3) {
          showList = that.data.completeList
        }
        that.setData({
          showList: showList
        })
      }
    })
  },
  // 菜品nav
  tabNav: function (e) {
    var that = this;
    var type = e.currentTarget.dataset.type;
    var showList=[];
    if (type == 0) {
      showList = that.data.noPayList
    }
    if (type == 1) {
      showList = that.data.waitDeliverList
    }
    if (type == 2) {
      showList = that.data.waitTakeDeliverList
    }
    if (type == 3) {
      showList=that.data.completeList
    }
    that.setData({
      currentTab: type,
      showList: showList
    })
  },
  // 
  navTo: function (e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    var data = that.data.showList[index]
    var url;
    var payList = {};
    var shopList = [];
    console.log(data);
    for (var i = 0; i < data.cart.length;i++){
      shopList.push({
        cart: data.cart[i],
        cover: data.dish[i].icon,
        dish: data.dish[i]
      })
    }
    payList = {
      shopList: shopList,
      total: data.order.totalFee,
      order: data.order
    }
    wx.setStorageSync("payList", payList);
    if (this.data.currentTab == 0){
      url = "../payment/payment?type=1";
      var Address = {
        name: data.order.name,
        phone: data.order.phone,
        add: data.order.address
      }
      wx.setStorageSync("Address", Address)          
    }else{
      url = "indentDetails"
    }
    wx.navigateTo({
      url: url,
    })
  },
  // 删除订单
  delOrder: function (e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    var showList = that.data.showList;
    var noPayList = that.data.noPayList;
    var waitDeliverList = that.data.waitDeliverList;
    var waitTakeDeliverList = that.data.waitTakeDeliverList;
    var completeList = that.data.completeList;
    var type = that.data.currentTab;
    console.log(e.currentTarget.dataset.index)
    wx.request({
      url: dealOrderUrl,
      method: "put",
      data: {
        skey: wx.getStorageSync('skey'),
        state: 9,
        outTradeNo: showList[index].order.outTradeNo
      },
      success: function (res) {
        if (res.data.code === 'ok') {
          if (type == 0) {
            showList.splice(index, 1);
            noPayList = showList;
          }
          if (type == 1) {
            showList.splice(index, 1);
            waitDeliverList = showList;
          }
          if (type == 2) {
            showList.splice(index, 1);
            waitTakeDeliverList = showList;
          }
          if (type == 3) {
            showList.splice(index, 1);
            completeList = showList;
          }

          that.setData({
            completeList: completeList,
            noPayList: noPayList,
            waitTakeDeliverList: waitTakeDeliverList,
            waitDeliverList: waitDeliverList,
            showList: showList
          })
        }
      },
    })
  },
})